import {
  Component,
  ChangeDetectionStrategy,
  OnInit,
  ChangeDetectorRef,
} from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
// import { STColumn } from '@delon/abc';
import { getTimeDistance } from '@delon/util';
import { _HttpClient } from '@delon/theme';
// import { I18NService } from '@core';
import { yuan } from '@shared';
import { format } from 'date-fns';

@Component({
  selector: 'app-dashboard-analysis',
  templateUrl: './analysis.component.html',
  styleUrls: ['./analysis.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DashboardAnalysisComponent implements OnInit {
  today;

  inter;
  loanAmount=1000; //历史借贷金额
  loanMonth='12'; //历史借贷金额月同比
  loanUp= 'up';//历史借贷金额的箭头
  xiajiatitle='待收货任务';
  tuisong='4月5号全厂停电，JIT业务适当安排加班';
  loanNalance = '126,560';//贷款余额
  loanNalanceWeek='12'; //贷款余额周同比
  loanNalanceWeekUp='down'; //贷款余额周同比箭头
  loanNalanceData='12'; //贷款余额日同比
  loanNalanceDataUp='up'; //贷款余额日同比箭头
  loanNalanceMonth='22'; //贷款余额月同比
  loanNalanceMonthUp='up'; //贷款余额月同比箭头

  loss='6,560';//损失金额
  lossMonth='12'; //损失金额月同比
  lossMonthUp='up'; //损失金额月同比箭头

  defective='30';//不良率
  defectiveMonth='12'; //不良率月同比
  defectiveMonthUp='up'; //不良率月同比箭头

  data: any = {};
  loading = true;
  date_range: Date[] = [];
  rankingListData: any[] = Array(7)
    .fill({})
    .map((item, i) => {
      return {
        title: "111",
        total: 323234,
      };
    });

  constructor(
    private http: _HttpClient,
    public msg: NzMessageService,
    // private i18n: I18NService,
    private cdr: ChangeDetectorRef,
  ) {}
  loanAmountData: any[] = [];//历史借贷金额
  lossData: any[] = [];//损失金额
  offlineData: any[] = [];//门店数据

  ngOnInit() {
    const beginDay = new Date().getTime();
    const fakeY = [7, 5, 4, 2, 4, 7, 5, 6, 5, 9, 6, 3, 1, 5, 3, 6, 5];
    //历史借贷金额
    for (let i = 0; i < fakeY.length; i += 1) {
      this.loanAmountData.push({
        x: format(new Date(beginDay + 1000 * 60 * 60 * 24 * i), 'YYYY-MM-DD'),
        y: fakeY[i],
      });
    }
    //损失金额
    for (let i = 0; i < fakeY.length; i += 1) {
      this.lossData.push({
        x: format(new Date(beginDay + 1000 * 60 * 60 * 24 * i), 'YYYY-MM-DD'),
        y: fakeY[i],
      });
    }
    //门店数据
    for (let i = 0; i < 10; i += 1) {
      this.offlineData.push({
        name: `门店${i}`,
        cvr: Math.ceil(Math.random() * 9) / 10,
      });
      this.cdr.detectChanges();
    }
    this.inter = setInterval(() => {
      this.today = new Date();// 或者this.today = new Date();
      this.tuisong=this.today;
      this
      console.log("1111");
    }, 1000);

  }

  setDate(type: any) {
    this.date_range = getTimeDistance(type);
    setTimeout(() => this.cdr.detectChanges());
  }

  salesType = 'all';
  salesPieData: any;
  salesTotal = 0;
  changeSaleType() {
    this.salesPieData =
      this.salesType === 'all'
        ? this.data.salesTypeData
        : this.salesType === 'online'
        ? this.data.salesTypeDataOnline
        : this.data.salesTypeDataOffline;
    if (this.salesPieData) {
      this.salesTotal = this.salesPieData.reduce((pre, now) => now.y + pre, 0);
    }
    this.cdr.detectChanges();
  }

  saleTabs: any[] = [
    { key: '进件数', show: true },
    { key: '拒单数' },
    { key: '放款数' },
    { key: '放款金额' }
  ];

  salesChange(idx: number) {
    if (this.saleTabs[idx].show !== true) {
      this.saleTabs[idx].show = true;
      this.cdr.detectChanges();
    }
  }
  ngOnDestroy() {
    if (this.inter) {
      clearInterval(this.inter);
    }
  }
  offlineIdx = 0;
  offlineChange(idx: number) {
    if (this.data.offlineData[idx].show !== true) {
      this.data.offlineData[idx].show = true;
      this.cdr.detectChanges();
    }
  }
}
